package com.qk.exception;

import com.qk.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler
    public Result handleException(Exception e){

        log.error("程序运行有问题",e);

        return Result.error("您当前的服务器有问题,请稍后在点击");

    }


    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException e){
       String message = e.getMessage();
       if (message.contains("Duplicate entry")) {
           String errorMessg = message.substring(message.indexOf("Duplicate entry"));
           String[] s = errorMessg.split(" ");
           return Result.error(s[2]+"已存在");

       }
       return Result.error("未知错误");

    }

    @ExceptionHandler
    public Result handleBusinnessException(BusinessException e){

        log.error("程序运行有问题",e);

        return Result.error(e.getMessage());

    }
}

